package top.hmtools.autoConfiguration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import top.hmtools.common.StaticContent;
import top.hmtools.manager.IRequestLoggerManager;
import top.hmtools.manager.RequestLoggerManagerDefault;

/**
 * 管理者实例配置
 * @author Jianghaibo
 *
 */
@Configuration
@ConditionalOnProperty(prefix=StaticContent.CONFIG_PREFIX,value=StaticContent.CONFIG_ITEM_KEY_NAME_ENABLED,matchIfMissing=true)
public class ManagerConfiguration {
    
    private static Logger logger = LoggerFactory.getLogger(ManagerConfiguration.class);
    
    @Value(value="${"+StaticContent.CONFIG_ITEM_KEY_NAME_REQUEST_LOGGER_MANAGER_NAME+":top.hmtools.manager.RequestLoggerManagerDefault}")
    private String requestLoggerManagerName;

    /**
     * 实例化一个日志信息管理者（写入数据库的策略类），缺省实例化 top.hmtools.manager.RequestLoggerManagerDefault
    * 方法说明：                    requestLoggerManager
    * 输入参数说明：           @return
    * 输出参数说明：           IRequestLoggerManager
    *
    *
     */
    @SuppressWarnings("unchecked")
    @Bean
    public IRequestLoggerManager requestLoggerManager(){
        Class<IRequestLoggerManager> clazz = null;
        try {
            clazz = (Class<IRequestLoggerManager>) Class.forName(requestLoggerManagerName);
            return clazz.newInstance();
        } catch (Exception e) {
        	e.printStackTrace();
            logger.error(e.getMessage(), e);
            return new RequestLoggerManagerDefault();
        }
    }
    
}
